package com.gs.wechatdemo.db.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.gs.wechatdemo.MyApplication;
import com.gs.wechatdemo.constant.AllConstants;
import com.gs.wechatdemo.db.bean.ChatBean;
import com.gs.wechatdemo.db.bean.ContactBean;
import com.gs.wechatdemo.util.DateUtil;
import com.gs.wechatdemo.util.SPUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class SQLiteUtil {
    private String path = Environment.getExternalStorageDirectory().getPath() + "/Android/data/"+MyApplication.getContext().getPackageName()+"/database/";
//    private String path = Environment.getExternalStorageDirectory().getPath() + "/wechatDemo/database/";
    private static final String DB_NAME = "chat_demo.db";
    public static final String TABLE_CONTACT = "contact_list";
    public static final String TABLE_CHAT = "chat_list";
    public static final String INDEX_CHAT = "chat_list_index";
    public static final String INDEX_CONTACT = "contact_list_index";
    public static final String INDEX_CONTACT_ID = "contact_list_id_index";
    private static final int VERSION = 1;
    private static volatile SQLiteUtil instance;
    private static SQLiteDatabase db;

    private SQLiteUtil() {
        if (db == null) {
            path = path + SPUtil.getInstance().getString(AllConstants.ID);
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            SQLiteOpenHelper mSqLiteOpenHelper = new SQLiteOpenHelper(MyApplication.getContext(), path + "/" + DB_NAME, null, VERSION);
            db = mSqLiteOpenHelper.getWritableDatabase();
        }
    }

    public static SQLiteUtil getInstance() {
        if (instance == null) {
            synchronized (SQLiteUtil.class) {
                if (instance == null) {
                    instance = new SQLiteUtil();
                }
            }
        }
        return instance;
    }

    public void logout(){
        instance = null;
        db = null;
    }

    public void insertContact(ContactBean bean) {
        ContentValues values = new ContentValues();
        values.put("userOrGroupId", bean.getUserOrGroupId());
        values.put("name", bean.getName());
        values.put("photo", bean.getPhoto());
        values.put("date", DateUtil.DateTimeToStr(bean.getDate(), DateUtil.FORMAT_DATE_TIME));
        values.put("content", bean.getContent());
        values.put("level", bean.getLevel());
        values.put("num", bean.getNum());
        db.insert(TABLE_CONTACT, null, values);
    }

    public List<ContactBean> queryContactList(String limit) {
        List<ContactBean> list = new ArrayList<>();
        Cursor cursor = db.query(TABLE_CONTACT, null, null, null, null, null, "date DESC", limit);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                ContactBean bean = new ContactBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setUserOrGroupId(cursor.getString(cursor.getColumnIndex("userOrGroupId")));
                bean.setName(cursor.getString(cursor.getColumnIndex("name")));
                bean.setPhoto(cursor.getString(cursor.getColumnIndex("photo")));
                bean.setDate(DateUtil.StrToDateTime(cursor.getString(cursor.getColumnIndex("date")), DateUtil.FORMAT_DATE_TIME));
                bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                bean.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
                bean.setNum(cursor.getInt(cursor.getColumnIndex("num")));
                list.add(bean);
            }
        }
        cursor.close();
        return list;
    }

    public ContactBean queryContactById(String userOrGroupId) {
        ContactBean bean = new ContactBean();
        Cursor cursor = db.query(TABLE_CONTACT, null, "userOrGroupId = ?", new String[]{userOrGroupId + ""}, null, null, null);
        if (cursor.moveToFirst()) {
            bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
            bean.setUserOrGroupId(cursor.getString(cursor.getColumnIndex("userOrGroupId")));
            bean.setName(cursor.getString(cursor.getColumnIndex("name")));
            bean.setPhoto(cursor.getString(cursor.getColumnIndex("photo")));
            bean.setDate(DateUtil.StrToDateTime(cursor.getString(cursor.getColumnIndex("date")), DateUtil.FORMAT_DATE_TIME));
            bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
            bean.setLevel(cursor.getInt(cursor.getColumnIndex("level")));
            bean.setNum(cursor.getInt(cursor.getColumnIndex("num")));
        }
        cursor.close();
        return bean;
    }

    public void updateContact(ContactBean bean) {
        db.execSQL("UPDATE " + TABLE_CONTACT + " SET date = ?," +
                        "content = ?," +
                        "num = ?" +
                        "WHERE userOrGroupId = ? AND level = ?",
                new String[]{DateUtil.DateTimeToStr(bean.getDate(), DateUtil.FORMAT_DATE_TIME),
                        bean.getContent(), bean.getNum() + "",
                        bean.getUserOrGroupId() + "", bean.getLevel() + ""});
    }

    public void deleteContact(int id) {
        db.delete(TABLE_CONTACT, "id = ?", new String[]{id + ""});
    }

    public void insertChat(ChatBean bean) {
        ContentValues values = new ContentValues();
        values.put("chatId", bean.getChatId());
        values.put("groupId", bean.getGroupId());
        values.put("userId", bean.getUserId());
        values.put("photo", bean.getPhoto());
        values.put("content", bean.getContent());
        values.put("photoW", bean.getPhotoW());
        values.put("photoH", bean.getPhotoH());
        values.put("date", DateUtil.DateTimeToStr(bean.getDate(), DateUtil.FORMAT_DATE_TIME));
        values.put("viewType", bean.getViewType());
        db.insert(TABLE_CHAT, null, values);
    }

    public List<ChatBean> queryChatList(String chatId, String limit) {
        List<ChatBean> list = new ArrayList<>();
        Cursor cursor = db.query(TABLE_CHAT, null, "chatId = ?", new String[]{chatId}, null, null, "id DESC", limit);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                ChatBean bean = new ChatBean();
                bean.setChatId(chatId);
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setGroupId(cursor.getString(cursor.getColumnIndex("groupId")));
                bean.setUserId(cursor.getString(cursor.getColumnIndex("userId")));
                bean.setPhoto(cursor.getString(cursor.getColumnIndex("photo")));
                bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                bean.setDate(DateUtil.StrToDateTime(cursor.getString(cursor.getColumnIndex("date")), DateUtil.FORMAT_DATE_TIME));
                bean.setViewType(cursor.getInt(cursor.getColumnIndex("viewType")));
                bean.setPhotoW(cursor.getInt(cursor.getColumnIndex("photoW")));
                bean.setPhotoH(cursor.getInt(cursor.getColumnIndex("photoH")));
                list.add(bean);
            }
        }
        cursor.close();
        return list;
    }

    public List<ChatBean> queryChatByContent(String content, String limit) {
        Cursor cursor = db.query(TABLE_CHAT, null, "content LIKE ?", new String[]{"%" + content + "%"}, null, null, "id DESC", limit);
        List<ChatBean> list = new ArrayList<>();
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                ChatBean bean = new ChatBean();
                bean.setId(cursor.getInt(cursor.getColumnIndex("id")));
                bean.setChatId(cursor.getString(cursor.getColumnIndex("chatId")));
                bean.setGroupId(cursor.getString(cursor.getColumnIndex("groupId")));
                bean.setUserId(cursor.getString(cursor.getColumnIndex("userId")));
                bean.setPhoto(cursor.getString(cursor.getColumnIndex("photo")));
                bean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                bean.setDate(DateUtil.StrToDateTime(cursor.getString(cursor.getColumnIndex("date")), DateUtil.FORMAT_DATE_TIME));
                bean.setViewType(cursor.getInt(cursor.getColumnIndex("viewType")));
                bean.setPhotoW(cursor.getInt(cursor.getColumnIndex("photoW")));
                bean.setPhotoH(cursor.getInt(cursor.getColumnIndex("photoH")));
                list.add(bean);
            }
        }
        cursor.close();
        return list;
    }

    public void deleteChatById(int id) {
        db.delete(TABLE_CHAT, "id = ?", new String[]{id + ""});
    }
}
